﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using SSTU.Extensions;
using SSTU.Model;
using SSTU.ViewModel.Core;

namespace SSTU.ViewModel.Item
{
	/// <summary>
	/// View model for week day.
	/// </summary>
	public class PeriodicWeekDayItemVM : ViewModelBase
	{
		private DayOfWeek? _day;

		/// <summary>
		/// Initializes a new instance of the <see cref="PeriodicWeekDayItemVM"/> class.
		/// </summary>
		public PeriodicWeekDayItemVM()
		{
			Day = null;
			Classes = new ObservableCollection<ClassItemVM>();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="PeriodicWeekDayItemVM"/> class.
		/// </summary>
		public PeriodicWeekDayItemVM(DayOfWeek? day, IEnumerable<Class> classes = null)
		{
			Day = day;
			Classes = new ObservableCollection<ClassItemVM>();
			if (classes != null)
			{
				var lessonsVM =
					from lesson in classes
					select new ClassItemVM(lesson.Start, lesson.End, lesson.RoomId, lesson.SubRoomId, lesson.BuildingOrOrganizationId, lesson.Theme, lesson.Lecturer);
				Classes.AddRange(lessonsVM);
			}
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="PeriodicWeekDayItemVM"/> class.
		/// </summary>
		public PeriodicWeekDayItemVM(DayOfWeek? day, IEnumerable<ClassItemVM> classes = null)
		{
			Day = day;
			Classes = new ObservableCollection<ClassItemVM>();
			if (classes != null)
			{
				Classes.AddRange(classes);
			}
		}

		/// <summary>
		/// Gets observable collection of lessons at this week day.
		/// </summary>
		public ObservableCollection<ClassItemVM> Classes
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets day of week.
		/// </summary>
		public DayOfWeek? Day
		{
			get { return _day; }
			set
			{
				if (value != _day)
				{
					RaisePropertyChanging<DayOfWeek?>(() => Day);
					RaisePropertyChanging<string>(() => DayString);
					_day = value;
					RaisePropertyChanged<DayOfWeek?>(() => Day);
					RaisePropertyChanged<string>(() => DayString);
				}
			}
		}

		/// <summary>
		/// Gets string value for day of week.
		/// </summary>
		public string DayString
		{
			get
			{
				string result = null;
				if (Day.HasValue)
				{
					switch (Day.Value)
					{
						case DayOfWeek.Friday:
							result = "Пятница";
							break;
						case DayOfWeek.Monday:
							result = "Понедельник";
							break;
						case DayOfWeek.Saturday:
							result = "Суббота";
							break;
						case DayOfWeek.Sunday:
							result = "Воскресенье";
							break;
						case DayOfWeek.Thursday:
							result = "Четверг";
							break;
						case DayOfWeek.Tuesday:
							result = "Вторник";
							break;
						case DayOfWeek.Wednesday:
							result = "Среда";
							break;
						default:
							result = "Повтосренье";
							break;
					}
				}
				else
				{
					result = null;
				}
				return result;
			}
		}
	}
}
